package com.fourfinance.model.service;

import java.text.ParseException;
import java.util.List;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.hibernate.SessionFactory;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.springframework.test.context.web.WebAppConfiguration;
import org.testng.annotations.AfterMethod;

import com.fourfinance.TestContext;
import com.fourfinance.TestUtil;
import com.fourfinance.exception.LenderException;
import com.fourfinance.model.dao.ClientDAO;
import com.fourfinance.model.domain.Client;

@ContextConfiguration(classes = {TestContext.class})
@WebAppConfiguration
public class UnitTestSupport extends AbstractTestNGSpringContextTests {

	@Resource
	protected SessionFactory sessionFactory;
	
	@Resource
	protected DataSource dataSource;

	@Resource
	protected ClientDAO clientDAO;
	
	@Resource
	protected ClientService clientService;
	
	@Resource
	protected IpService ipService;
	
	@Resource
	protected LoanService loanService;
	
	protected void loadClients() throws LenderException{
		List<Client> clients = TestUtil.getMockClients();
		for (Client client : clients) {
			clientService.add(client);
		}
	}
	
	/**
	 * Cleaning-up DB. Cascade deleting the clients.
	 * 
	 * @throws ParseException
	 */
	@AfterMethod
	public void cleanDB() throws ParseException{
		List<Client> clientList = clientService.list();
		for (Client client : clientList) {
			clientService.remove(client.getId());
		}
	}	
	
}
